package com.sq.partner.manager.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.log4j.Logger;


public class HttpConnectorUtil {
	private static Logger logger = LogUtil.getLog(HttpConnectorUtil.class);
	public static enum HttpMethod {GET, POST}
	/**
	 * 请求交易系统
	 * @param requestUrl 请求接口
	 * @param requestXml 请求报文
	 * @param GW_CH_CODE
	 * @param GW_CH_TX
	 * @return 返回报文，失败为""
	 */
	public static String interaction(String requestUrl,String requestXml,String GW_CH_CODE,String GW_CH_TX){
		StringBuffer responseXml = new StringBuffer();
		HttpURLConnection httpConn = null;
		OutputStream out = null;
		BufferedReader in = null;
		URL url = null;
		try {
			url = new URL(requestUrl);
			httpConn = (HttpURLConnection)url.openConnection();
			byte[] buf= requestXml.getBytes("GBK");
			httpConn.setRequestProperty("Content-Length",String.valueOf(buf.length));
			httpConn.setRequestProperty("Content-Type","text/xml; charset=gbk");
			httpConn.setRequestProperty("GW_FACADE_FLAG","1");
			httpConn.setRequestProperty("GW_CH_TX",GW_CH_TX);
			GW_CH_CODE = GW_CH_CODE == null?"":GW_CH_CODE;
			httpConn.setRequestProperty("GW_CH_USER",GW_CH_CODE);
			httpConn.setRequestProperty("GW_CH_PWD",GW_CH_CODE);
			httpConn.setRequestProperty("GW_CH_CODE",GW_CH_CODE);
			httpConn.setRequestMethod("POST");
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setConnectTimeout(60000);  
			httpConn.setReadTimeout(60000);
			out = httpConn.getOutputStream();
			out.write(buf);
			out.flush();
			in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"gbk"));
			String inputLine = "";
			while((inputLine = in.readLine()) != null) {
				responseXml.append(inputLine);
			}
		}catch(IOException e){
			logger.error("交易系统请求失败:",e);
		}finally{
			close(out);
			close(in);
			close(httpConn);
		}
		return responseXml.toString();
	}
	private static void close(HttpURLConnection httpConn) {
		if (httpConn != null) {
    			httpConn.disconnect();
    	}
	}
	
	/**
	 * 请求影像系统使用utf-8编码,http post 请求
	 * */
	public static String interaction(String requestUrl,String requestXml){
		String responseXml="";
		OutputStream output=null;
		   try{
			        HttpURLConnection hc = null;
			        URL url = new URL(requestUrl);
					hc = (HttpURLConnection)url.openConnection();
					// 设置连接参数
					hc.setRequestMethod("POST");
					hc.setRequestProperty("content-type", "text/html");
					hc.setUseCaches(false);
					hc.setDoOutput(true);
					hc.setDoInput(true);
					hc.connect();
				    output = hc.getOutputStream();
					output.write(requestXml.getBytes("UTF-8"), 0, requestXml.getBytes("UTF-8").length);
					output.flush();
					StringBuffer bf = new StringBuffer();
					InputStream stream = hc.getInputStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
					String line = "";
					while ((line = reader.readLine()) != null) {
						bf.append(line);
					}
					if(bf.length()>0){
						responseXml=bf.toString();
					}
					
				} catch (Exception e) {
					logger.info(e);
				}finally{
					if(output!=null){
						try {
							output.close();
			    		}catch (IOException e) {
							// TODO Auto-generated catch block
			    			logger.error("释放OutputStream失败",e);
						}
					}
				}
		   
	     return responseXml;
	}
//	public static HttpURLConnection getHttpURLConnecttion(String urlString){
//		
//		URL url;
//		HttpURLConnection httpConn=null;
//		try {
//			url = new URL(urlString);
//			httpConn = (HttpURLConnection)url.openConnection();
//		} catch (IOException e) {
//			return null;
//		}finally{
//			if(null!=httpConn){
//				httpConn.disconnect();	
//			}
//		}
//		
//		return httpConn;
//	}
	public static void close(OutputStream out) {
    	if (out != null) {
    		try {
    			out.close();
    		}catch (IOException e) {
				// TODO Auto-generated catch block
    			logger.error("释放OutputStream失败",e);
			}
    	}
	}
	public static void close(BufferedReader in) {
    	if (in != null) {
    		try {
    			in.close();
    		}catch (IOException e) {
				// TODO Auto-generated catch block
    			logger.error("释放BufferedReader失败",e);
			}
    	}
	}
}
